package com.service.tools.controller.api;

import com.service.tools.componets.JwtTokenUtil;
import com.service.tools.componets.PasswordUtil;
import com.service.tools.entity.Result;
import com.service.tools.entity.ToolUser;
import com.service.tools.service.IToolUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class AuthController {

    @Autowired
    private IToolUserService userService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private PasswordUtil passwordUtil;

    /**
     * 用户登录接口
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) {
        try {
            // 根据用户名查找用户
            ToolUser user = userService.getByUserName(loginRequest.getUsername());
            
            if (user == null) {
                return Result.error("用户不存在");
            }

            // 验证密码
            boolean isPasswordValid = passwordUtil.verifyPassword(
                loginRequest.getPassword(), 
                user.getSalt(), 
                user.getPassword()
            );

            if (!isPasswordValid) {
                return Result.error("密码错误");
            }

            // 生成token
            String token = jwtTokenUtil.generateToken(user);

            // 返回成功响应
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "登录成功");
            response.put("token", token);
            response.put("user", createUserResponse(user));

            return Result.success("登录成功",response);

        } catch (Exception e) {
            return Result.error("登录失败: " + e.getMessage());
        }
    }

    private Map<String, Object> createErrorResponse(String message) {
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("success", false);
        errorResponse.put("message", message);
        return errorResponse;
    }

    private Map<String, Object> createUserResponse(ToolUser user) {
        Map<String, Object> userResponse = new HashMap<>();
        userResponse.put("id", user.getId());
        userResponse.put("userName", user.getUserName());
        userResponse.put("createTime", user.getCreateTime());
        return userResponse;
    }

    /**
     * 登录请求参数类
     */
    public static class LoginRequest {
        private String username;
        private String password;

        // Getters and Setters
        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}
